﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MusicService
{
    public partial class MusicService : ICommentService
    {
        public bool addComment(string commentContent, DateTime createDate, string userID, string trackID)
        {
            bool flag = false;
            Comment comment = new Comment();
            try
            {
                long numberComment = dc.Comments.Count() + 1;
                comment.CommentID = "COMMENT" + numberComment;
                comment.CommentContent = commentContent;
                comment.CreateDate = createDate;
                comment.UserID = userID;
                comment.TrackD = trackID;
                dc.Comments.InsertOnSubmit(comment);
                dc.SubmitChanges();
                flag = true;
            }
            catch (Exception)
            {

            }
            return flag;
            
        }

        public bool removeComment(string commentID)
        {
            bool flag = false;
            try
            {
                Comment comment = dc.Comments.Single(tmp => tmp.CommentID.Equals(commentID));
                if (comment != null)
                {
                    dc.Comments.DeleteOnSubmit(comment);
                    dc.SubmitChanges();
                    flag = true;
                }
            }
            catch (Exception)
            {

            }
            return flag;
        }

        public bool updateComment(string commentID,string commentContent, DateTime createDate, string userID, string trackID)
        {
            bool flag = false;
            try
            {
                Comment comment = dc.Comments.Single(tmp => tmp.CommentID.Equals(commentID));
                if (comment != null)
                {
                    comment.CommentContent = commentContent;
                    comment.CreateDate = createDate;
                    comment.UserID = userID;
                    comment.TrackD = trackID;
                    dc.Comments.InsertOnSubmit(comment);
                    dc.SubmitChanges();
                    flag = true;
                }
                
            }
            catch (Exception)
            {

            }
            return flag;
        }

        public List<Comment> getAllComment()
        {
            var list = from c in dc.Comments select c;
            return list.ToList();
        }

        public Comment getRoleByTrackID(string trackID)
        {
            Comment comment = dc.Comments.Single(tmp => tmp.TrackD.Equals(trackID));
            return comment;
        }
    }
}